package com.stud.dustteam.dao;

import com.stud.dustteam.model.User;
import com.stud.dustteam.util.HibernateUtil;
import org.hibernate.classic.Session;

/**
 * Created with IntelliJ IDEA.
 * User: Shady
 * Date: 11.11.12
 * Time: 18:17
 * To change this template use File | Settings | File Templates.
 */
public class UserDAO implements DAO <User> {

    @Override
    public void create(User user) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

    @Override
    public User load(Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id);
        session.getTransaction().commit();
        return user;
    }

    @Override
    public void delete(Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id);
        if(null != user) {
            session.delete(user);
        }
        session.getTransaction().commit();
    }

    @Override
    public void update(User user) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(user);
        session.getTransaction().commit();
    }
}
